{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "go a message: Hello world!\n"
     ]
    }
   ],
   "source": [
    "def func(message):\n",
    "    return 'go a message: {}'.format(message)\n",
    "send_message = func\n",
    "print(func('Hello world!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "go a message: Hello world\n"
     ]
    }
   ],
   "source": [
    "def get_message(message):\n",
    "    return 'go a message: {}'.format(message)\n",
    "def root_call(func,message):\n",
    "    print(func(message))\n",
    "\n",
    "root_call(get_message,'Hello world')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got a message:Hello world!\n"
     ]
    }
   ],
   "source": [
    "def func(message):\n",
    "    def get_message(message):\n",
    "        print('Got a message:{}'.format(message))\n",
    "    return get_message(message)\n",
    "func('Hello world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got a message:Hello world!\n"
     ]
    }
   ],
   "source": [
    "def func_closure():\n",
    "    def get_message(message):\n",
    "        print('Got a message:{}'.format(message))\n",
    "    return get_message\n",
    "send_message = func_closure()\n",
    "send_message('Hello world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrapper of decorator\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 简单的装饰器\n",
    "def my_decorate(func):\n",
    "    def wrapper():\n",
    "        print('wrapper of decorator')\n",
    "        func()\n",
    "    return wrapper\n",
    "def greet():\n",
    "    print('hello world')\n",
    "greet = my_decorate(greet)\n",
    "greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrapper of decorator\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 简单的装饰器\n",
    "def my_decorate(func):\n",
    "    def wrapper():\n",
    "        print('wrapper of decorator')\n",
    "        func()\n",
    "    return wrapper\n",
    "@my_decorate\n",
    "def greet():\n",
    "    print('hello world')\n",
    "\n",
    "greet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrapper of decorator\n",
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "# 带参数的装饰器\n",
    "# 简单的装饰器\n",
    "def my_decorate(func):\n",
    "    def wrapper(message):\n",
    "        print('wrapper of decorator')\n",
    "        func(message)\n",
    "    return wrapper\n",
    "@my_decorate\n",
    "def greet(message):\n",
    "    print(message)\n",
    "\n",
    "greet('Hello world!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrapper of decorater\n",
      "Hello world\n",
      "wrapper of decorater\n",
      "Hello world\n",
      "wrapper of decorater\n",
      "Hello world\n",
      "wrapper of decorater\n",
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "# 带有自定义参数的装饰器\n",
    "def repeat(num):\n",
    "    def my_decorate(func):\n",
    "        @functools.wraps(func)   # 保留元函数的元信息\n",
    "        def wrapper(*args,**kwargs):\n",
    "            for i in range(num):\n",
    "                print('wrapper of decorater')\n",
    "                func(*args,**kwargs)\n",
    "        return wrapper\n",
    "    return my_decorate\n",
    "@repeat(4)\n",
    "def greet(message):\n",
    "    print(message)\n",
    "greet('Hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'greet'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "greet.__name__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute decorated1\n",
      "execute decorated2\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "# 装饰器的嵌套\n",
    "import functools\n",
    "def my_decorate1(func):\n",
    "    @functools.wraps(func)   # 保留元函数的元信息\n",
    "    def wrapper(*args,**kwargs):\n",
    "        print('execute decorated1')\n",
    "        func(*args,**kwargs)\n",
    "    return wrapper\n",
    "\n",
    "def my_decorate2(func):\n",
    "    @functools.wraps(func)   # 保留元函数的元信息\n",
    "    def wrapper(*args,**kwargs):\n",
    "        print('execute decorated2')\n",
    "        func(*args,**kwargs)\n",
    "    return wrapper\n",
    "@my_decorate1\n",
    "@my_decorate2\n",
    "def greet(message):\n",
    "    print(message)\n",
    "greet('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "caculate_similarity took 1.00000761449337 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "import functools\n",
    "def log_excution_time(func):\n",
    "    @functools.wraps(func)\n",
    "    def wrapper(*args,**kwargs):\n",
    "        start = time.perf_counter()\n",
    "        res = func(*args,**kwargs)\n",
    "        end = time.perf_counter()\n",
    "        print('{} took {} ms'.format(func.__name__,(end-start)*1000*1000))\n",
    "        return res\n",
    "    return wrapper\n",
    "@log_excution_time\n",
    "def caculate_similarity(items):\n",
    "    return len(items)\n",
    "arr = [1,2,3,4]\n",
    "caculate_similarity(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90035.6560632\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "print(time.perf_counter())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
